﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Fantasy_Fall_Level_Editor
{
    public partial class Heightmap_Window : Form
    {
        Tile selectedTile;

        Size tileSize;

        Bitmap tileSet;

        Point offset;

        int scale = 16;

        enum Mode { ADD_MODE, REMOVE_MODE, CLEAR_MODE };

        Mode mode = Mode.ADD_MODE;

        public event EventHandler Window_Closing;

        public Tile Heightmap_Window_SelectedTile
        {
            get { return selectedTile; }
            set { selectedTile = value; }
        }

        public Size Heightmap_Window_TileSize
        {
            get{return tileSize;}
            set{tileSize = value;}
        }

        public Bitmap Heightmap_Window_TileSet
        {
            get { return tileSet; }
            set { tileSet = value; }
        }

        public Point Heightmap_Window_Offset
        {
            get { return offset; }
            set { offset = value; }
        }

        public Heightmap_Window()
        {
            InitializeComponent();
        }

        private void Heightmap_Window_Closing(object sender, EventArgs e)
        {
            if(Window_Closing != null)
            {
                Window_Closing(this, EventArgs.Empty);
            }
        }

        private void Add_Pixel_Button_Click(object sender, EventArgs e)
        {
            mode = Mode.ADD_MODE;

            Add_Pixel_Button.Checked = true;
            Remove_Pixel_Button.Checked = false;
            Clear_Button.Checked = false;
        }

        private void Remove_Pixel_Button_Click(object sender, EventArgs e)
        {
            mode = Mode.REMOVE_MODE;

            Remove_Pixel_Button.Checked = true;
            Add_Pixel_Button.Checked = false;
            Clear_Button.Checked = false;
        }

        private void Clear_Button_Click(object sender, EventArgs e)
        {
            mode = Mode.CLEAR_MODE;

            Clear_Button.Checked = true;
            Add_Pixel_Button.Checked = false;
            Remove_Pixel_Button.Checked = false;
        }

        private void Heightmap_Window_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (Window_Closing != null)
            {
                Window_Closing(this, EventArgs.Empty);
            }
        }

        private void HeightmapPanel_Paint(object sender, PaintEventArgs e)
        {
            Rectangle destRect = Rectangle.Empty;

            destRect.Size = new Size(tileSize.Width * scale, tileSize.Height * scale);

            RectangleF srcRect = RectangleF.Empty;
            srcRect.X += (selectedTile.X * tileSize.Width) - 0.5f;
            srcRect.Y += (selectedTile.Y * tileSize.Height) - 0.5f;
            srcRect.Size = tileSize;

            e.Graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
            e.Graphics.DrawImage(tileSet, destRect, srcRect, GraphicsUnit.Pixel);

            for(int x = 0; x < tileSize.Width; x++)
            {
                for(int y = 0; y < tileSize.Height; y++)
                {
                    destRect = Rectangle.Empty;
                    destRect.X = x * scale;
                    destRect.Y = y * scale;
                    destRect.Size = new Size(scale, scale);

                    e.Graphics.DrawRectangle(Pens.Black, destRect);

                }
            }

            Brush heightBrush = new SolidBrush(Color.FromArgb(125, Color.Green));

            for(int x = 0; x < tileSize.Width; x++)
            {
                int y = selectedTile.HeightMap[x];
                e.Graphics.FillRectangle(heightBrush, x * scale, y * scale, scale, scale);
            }
        }

        private void HeightmapPanel_MouseClick(object sender, MouseEventArgs e)
        {

            if(mode == Mode.ADD_MODE)
            {
                Add_Click(sender, e);
            }

            else if(mode == Mode.REMOVE_MODE)
            {
                Remove_Click(sender, e);
            }

            else if(mode == Mode.CLEAR_MODE)
            {
                selectedTile.HeightMap = new int[tileSize.Width];

                for(int i = 0; i < tileSize.Width; i++)
                {
                    selectedTile.HeightMap[i] = -1;
                }
            }

            HeightmapPanel.Invalidate();
        }

        private void Add_Click(object sender, MouseEventArgs e)
        {
            Rectangle rect;

            for(int x = 0; x < tileSize.Width; x++)
            {
                for(int y = 0; y < tileSize.Height; y++)
                {
                    rect = Rectangle.Empty;
                    rect.X += x * scale;
                    rect.Y += y * scale;
                    rect.Size = new Size(scale, scale);

                    if(rect.Contains(e.Location))
                    {
                        selectedTile.HeightMap[x] = y;
                    }
                }
            }
        }

        private void Remove_Click(object sender, MouseEventArgs e)
        {
            Rectangle rect;

            for (int x = 0; x < tileSize.Width; x++)
            {
                for (int y = 0; y < tileSize.Height; y++)
                {
                    rect = Rectangle.Empty;
                    rect.X += scale * x;
                    rect.Y += scale * y;
                    rect.Size = new Size(scale, scale);

                    if (rect.Contains(e.Location))
                    {
                        selectedTile.HeightMap[x] = -1;
                    }
                }
            }
        }

        private void HeightmapPanel_MouseMove(object sender, MouseEventArgs e)
        {
            if (MouseButtons.Left == e.Button)
            {
                HeightmapPanel_MouseClick(sender, e);
            }
        }
    }
}
